package com.qianhui.dao;

import com.qianhui.common.CM;
import com.qianhui.common.IResult;
import com.qianhui.common.IStatus;
import com.qianhui.pojo.*;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Order
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Order
 */
public class OrderDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(OrderDAO.class);
    // property constants
    public static final String ORDER_TOTAL_PRICE = "orderTotalPrice";
    public static final String ORDER_FREIGHT = "orderFreight";
    public static final String ORDER_NAME = "orderName";
    public static final String ORDER_BUYER_NAME = "orderBuyerName";
    public static final String ORDER_BUYER_PHONE = "orderBuyerPhone";
    public static final String ORDER_BUYER_ADDRESS = "orderBuyerAddress";
    public static final String ORDER_BUYER_REAL_NAME = "orderBuyerRealName";
    public static final String ORDER_BUYER_PEOPLE_ID = "orderBuyerPeopleId";
    public static final String ORDER_RECEIVE_WAY = "orderReceiveWay";
    public static final String ORDER_PAY_WAY = "orderPayWay";
    public static final String ORDER_ONLINE_TRADE_NO = "orderOnlineTradeNo";
    public static final String BUYER_ID = "buyerId";
    public static final String OPERATION_MODE = "operationMode";
    public static final String ORDER_STATUS = "orderStatus";
    public static final String ORDER_EXPRESS_COMPANY = "orderExpressCompany";
    public static final String ORDER_EXPRESS_NUMBER = "orderExpressNumber";

    public Order save(Order transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<Order> saveList(List<Order> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (Order temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(Order persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Order findById(java.lang.String id)
    {
        log.debug("getting Order instance with id: " + id);
        try
        {
            Order instance = (Order) getSession().get("com.qianhui.pojo.Order", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Order instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Order as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByOrderTotalPrice(Object orderTotalPrice)
    {
        return findByProperty(ORDER_TOTAL_PRICE, orderTotalPrice);
    }

    public List findByOrderFreight(Object orderFreight)
    {
        return findByProperty(ORDER_FREIGHT, orderFreight);
    }

    public List findByOrderName(Object orderName)
    {
        return findByProperty(ORDER_NAME, orderName);
    }

    public List findByOrderBuyerName(Object orderBuyerName)
    {
        return findByProperty(ORDER_BUYER_NAME, orderBuyerName);
    }

    public List findByOrderBuyerPhone(Object orderBuyerPhone)
    {
        return findByProperty(ORDER_BUYER_PHONE, orderBuyerPhone);
    }

    public List findByOrderBuyerAddress(Object orderBuyerAddress)
    {
        return findByProperty(ORDER_BUYER_ADDRESS, orderBuyerAddress);
    }

    public List findByOrderBuyerRealName(Object orderBuyerRealName)
    {
        return findByProperty(ORDER_BUYER_REAL_NAME, orderBuyerRealName);
    }

    public List findByOrderBuyerPeopleId(Object orderBuyerPeopleId)
    {
        return findByProperty(ORDER_BUYER_PEOPLE_ID, orderBuyerPeopleId);
    }

    public List findByOrderReceiveWay(Object orderReceiveWay)
    {
        return findByProperty(ORDER_RECEIVE_WAY, orderReceiveWay);
    }

    public List findByOrderPayWay(Object orderPayWay)
    {
        return findByProperty(ORDER_PAY_WAY, orderPayWay);
    }

    public List findByOrderOnlineTradeNo(Object orderOnlineTradeNo)
    {
        return findByProperty(ORDER_ONLINE_TRADE_NO, orderOnlineTradeNo);
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByOperationMode(Object operationMode)
    {
        return findByProperty(OPERATION_MODE, operationMode);
    }

    public List findByOrderStatus(Object orderStatus)
    {
        return findByProperty(ORDER_STATUS, orderStatus);
    }

    public List findByOrderExpressCompany(Object orderExpressCompany)
    {
        return findByProperty(ORDER_EXPRESS_COMPANY, orderExpressCompany);
    }

    public List findByOrderExpressNumber(Object orderExpressNumber)
    {
        return findByProperty(ORDER_EXPRESS_NUMBER, orderExpressNumber);
    }

    public List findAll()
    {
        log.debug("finding all Order instances");
        try
        {
            String queryString = "from Order";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List<Order> confirm_order_addOrderAndOrderGoodsListAndDeleteCartListAndSaveBuyer(List<Order> orderList, List<OrderGoods> orderGoodsList, List<Cart> cartListNeedDelete, BuyerCoupon buyerCoupon, Buyer buyer)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();

            for (OrderGoods orderGoods : orderGoodsList)
            {
                getSession().saveOrUpdate(orderGoods);
                Goods goodsTemp = (Goods) getSession().get(Goods.class, orderGoods.getGoodsId());
                goodsTemp.setGoodsInventoryNumber(goodsTemp.getGoodsInventoryNumber() - orderGoods.getGoodsNumber());
                getSession().saveOrUpdate(goodsTemp);
            }

            for (Order order : orderList)
            {
                getSession().saveOrUpdate(order);
            }

            if (buyerCoupon != null)
            {
                getSession().saveOrUpdate(buyerCoupon);
            }

            if (buyer != null)
            {
                getSession().saveOrUpdate(buyer);
            }

            for (Cart cart : cartListNeedDelete)
            {
                getSession().delete(cart);
            }

            tx.commit();
        } catch (Exception e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return orderList;
    }

    public List findByIdsAndBuyerId(List<String> ids, String buyerId)
    {
        try
        {
            String sql = "from Order o where o.orderId in (:ids) and o.buyerId=:buyerId order by o.orderBuyTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameterList("ids", ids);
            query.setParameter("buyerId", buyerId);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public Order findByIdAndBuyerId(String id, String buyerId)
    {
        try
        {
            String sql = "from Order o where o.orderId=:id and o.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("id", id);
            query.setParameter("buyerId", buyerId);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (Order) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByBuyerIdAndIndexAndPageSizeAndStatus(String buyerId, int index, int pageSize, String status)
    {
        try
        {
            String sql = "from Order o where o.buyerId=:buyerId and o.orderStatus=:status order by o.orderBuyTime :orderByStr";
            switch (status)
            {
                case IStatus.ORDER_TO_BE_PAYED:
                case IStatus.ORDER_TO_BE_SEND:
                case IStatus.ORDER_TO_BE_RECEIVED:
                    sql = sql.replaceAll(":orderByStr", IStatus.ORDER_BY_ASC);
                    break;
                case IStatus.ORDER_TO_BE_COMMENTED:
                case IStatus.ORDER_RETURN_PROCESSING:
                case IStatus.ORDER_HAVE_BEEN_COMPLETED:
                case IStatus.ORDER_HAVE_BEEN_CANCELLED:
                    sql = sql.replaceAll(":orderByStr", IStatus.ORDER_BY_DESC);
                    break;
                default:
                    sql = sql.replaceAll(":orderByStr", IStatus.ORDER_BY_ASC);
                    break;
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("status", status);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerIdAndStatus(String buyerId, String status)
    {
        try
        {
            String sql = "select count(*) from Order o where o.buyerId=:buyerId and o.orderStatus=:status";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("status", status);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListCompleteOrderByBuyerIdAndIndexAndPageSizeAndBeginTimeAndEndTime(String buyerId, int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from Order o where o.buyerId=:buyerId and o.orderStatus=:status and o.orderCompleteTime between :beginTime and :endTime order by o.orderCompleteTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("status", IStatus.ORDER_HAVE_BEEN_COMPLETED);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListPayedOrderByBuyerIdAndIndexAndPageSizeAndBeginTimeAndEndTime(String buyerId, int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from Order o where o.buyerId=:buyerId and o.orderPayTime between :beginTime and :endTime order by o.orderPayTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountCompleteOrderByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Order o where o.buyerId=:buyerId and o.orderStatus=:status and o.orderCompleteTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("status", IStatus.ORDER_HAVE_BEEN_COMPLETED);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListToBePayedOrderByBuyerIdAndIndexAndPageSizeAndBeginTimeAndEndTime(String buyerId, int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from Order o where o.buyerId=:buyerId and o.orderStatus=:status and o.orderBuyTime between :beginTime and :endTime order by o.orderBuyTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("status", IStatus.ORDER_TO_BE_PAYED);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountToBePayedOrderByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Order o where o.buyerId=:buyerId and o.orderStatus=:status and o.orderBuyTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("status", IStatus.ORDER_TO_BE_PAYED);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByBuyerIdAndIndexAndPageSize(String buyerId, int index, int pageSize)
    {
        try
        {
            String sql = "from Order o where o.buyerId=:buyerId order by o.orderBuyTime desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerId(String buyerId)
    {
        try
        {
            String sql = "select count(*) from Order o where o.buyerId=:buyerId";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List<Order> batchCancelOrder(List<Order> orderList, List<Goods> goodsList)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();

            for (Order order : orderList)
            {
                getSession().saveOrUpdate(order);
            }
            for (Goods goods : goodsList)
            {
                getSession().saveOrUpdate(goods);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return orderList;
    }

    public List getListByIndexAndPageSizeAndStatusAnd_keyword(int index, int pageSize, String status, String keyword)
    {
        try
        {
            String sql = "from Order o where o.orderStatus=:status :kSql order by o.orderBuyTime :orderByStr";
            switch (status)
            {
                case IStatus.ORDER_TO_BE_PAYED:
                case IStatus.ORDER_TO_BE_SEND:
                case IStatus.ORDER_TO_BE_RECEIVED:
                    sql = sql.replaceAll(":orderByStr", IStatus.ORDER_BY_ASC);
                    break;
                case IStatus.ORDER_TO_BE_COMMENTED:
                case IStatus.ORDER_RETURN_PROCESSING:
                case IStatus.ORDER_HAVE_BEEN_COMPLETED:
                case IStatus.ORDER_HAVE_BEEN_CANCELLED:
                    sql = sql.replaceAll(":orderByStr", IStatus.ORDER_BY_DESC);
                    break;
                default:
                    sql = sql.replaceAll(":orderByStr", IStatus.ORDER_BY_ASC);
                    break;
            }
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "and (o.orderId like :keyword or o.orderName like :keyword or o.buyerId like :keyword or o.orderBuyerName like :keyword or o.orderBuyerPhone like :keyword or o.orderBuyerAddress like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByStatusAnd_keyword(String status, String keyword)
    {
        try
        {
            String sql = "select count(*) from Order o where o.orderStatus=:status :kSql";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "and (o.orderId like :keyword or o.orderName like :keyword or o.buyerId like :keyword or o.orderBuyerName like :keyword or o.orderBuyerPhone like :keyword or o.orderBuyerAddress like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            query.setParameter("status", status);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByIndexAndPageSizeAndKeyword(int index, int pageSize, String keyword)
    {
        try
        {
            String sql = "from Order o :kSql order by o.orderBuyTime desc";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "where (o.orderId like :keyword or o.orderName like :keyword or o.buyerId like :keyword or o.orderBuyerName like :keyword or o.orderBuyerPhone like :keyword or o.orderBuyerAddress like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByKeyword(String keyword)
    {
        try
        {
            String sql = "select count(*) from Order o :kSql";
            if (CM.validIsEmpty(keyword) == true)
            {
                sql = sql.replace(":kSql", " ");
            }
            else
            {
                sql = sql.replace(":kSql", "where (o.orderId like :keyword or o.orderName like :keyword or o.buyerId like :keyword or o.orderBuyerName like :keyword or o.orderBuyerPhone like :keyword or o.orderBuyerAddress like :keyword)");
            }
            Query query = getSession().createQuery(sql);
            if (CM.validIsEmpty(keyword) == false)
            {
                query.setParameter("keyword", "%" + keyword + "%");
            }
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public Order post_order_and_post_comments(Order order, List<OrderGoods> orderGoodsList, List<Comment> commentList)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();

            getSession().saveOrUpdate(order);

            for (OrderGoods orderGoods : orderGoodsList)
            {
                getSession().saveOrUpdate(orderGoods);
            }

            for (Comment comment : commentList)
            {
                getSession().saveOrUpdate(comment);
            }

            tx.commit();
        } catch (Exception e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return order;
    }

    public double getTotalMoneyByBuyerIdAndBeginTimeAndEndTimeAndOperationMode(String buyerId, Date beginTime, Date endTime, String operationMode)
    {
        try
        {
            String sql = "select sum(o.orderTotalPrice) from Order o where o.buyerId=:buyerId and o.operationMode=:operationMode and o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("operationMode", operationMode);
            query.setTimestamp("beginTime", beginTime);
            query.setTimestamp("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).doubleValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public double getTotalMoneyByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(o.orderTotalPrice) from Order o where o.buyerId=:buyerId and o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setTimestamp("beginTime", beginTime);
            query.setTimestamp("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).doubleValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public double getTotalMoneyByPeopleIdAndBeginTimeAndEndTime(String peopleId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(o.orderTotalPrice) from Order o where o.orderBuyerPeopleId=:peopleId and o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("peopleId", peopleId);
            query.setTimestamp("beginTime", beginTime);
            query.setTimestamp("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).doubleValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


    public double getTotalTaxByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select sum(o.orderTax) from Order o where o.buyerId=:buyerId and o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setTimestamp("beginTime", beginTime);
            query.setTimestamp("endTime", endTime);

            List li = query.list();
            if (li == null || li.isEmpty())
            {
                return 0;
            }
            else if (li.get(0) == null)
            {
                return 0;
            }
            else
            {
                return ((Number) li.get(0)).doubleValue();
            }

        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }


    public List getListPayedOrderByIndexAndPageSizeAndBeginTimeAndEndTime(int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from Order o where o.orderPayTime between :beginTime and :endTime order by o.orderPayTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountPayedOrderByBeginTimeAndEndTime(Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Order o where o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountPayedOrderByBuyIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Order o where o.buyerId=:buyerId and o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountPayedOrderByPeopleIdAndBeginTimeAndEndTime(String peopleId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Order o where o.orderBuyerPeopleId=:peopleId and o.orderPayTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("peopleId", peopleId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListCompleteOrderByIndexAndPageSizeAndBeginTimeAndEndTime(int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from Order o where o.orderCompleteTime between :beginTime and :endTime order by o.orderCompleteTime asc";
            Query query = getSession().createQuery(sql);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountCompleteOrderByBeginTimeAndEndTime(Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from Order o where o.orderCompleteTime between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}